<?php

use GuzzleHttp\Client;

// 实名认证接口
class Authentication
{
    private $id_card;
    private $phone;
    private $name;

    public function __construct($param = [])
    {
        $this->id_card = $param['id_card'];
        $this->phone = $param['phone'];
        $this->name = $param['name'];
    }

    /**
     * 实名认证
     * @throws Exception
     */
    public function realName()
    {
        $config = require_once __DIR__ . '/config.php';
        try {
            $response = (new Client())->post($config['url'], [
                'form_params' => [
                    'idcard' => $this->id_card,
                    'mobile' => $this->phone,
                    'name' => $this->name
                ],
                'timeout' => 3,
                'headers' => [
                    'Authorization' => 'APPCODE ' . $config['appcode'],
                    'Content-Type' => "application/x-www-form-urlencoded; charset=UTF-8"
                ],
                'debug' => false
            ]);
            return json_decode((string)$response->getBody(), true);
        } catch (\GuzzleHttp\Exception\ClientException $exception) {
            $responseBody = json_decode($exception->getResponse()->getBody()->getContents(), true);
            throw new Exception($responseBody['code'] . '#' . $responseBody['message']);
        }
    }
}